package com.easy.util;

import jakarta.mail.*;
import jakarta.mail.internet.InternetAddress;
import jakarta.mail.internet.MimeMessage;

import java.util.Properties;

public class EmailUtil {
    private static final String FROM = "2955097528@qq.com";
    private static final String AUTI_CODE="puuaelmeoofzdegc";
    private static final String HOST = "smtp.qq.com";
    private static final String PORT = "465";
    private static final Session session;
    static{
        Properties props = new Properties();
        props.put("mail.smtp.host", HOST);
        props.put("mail.smtp.port", PORT);
        props.put("mail.smtp.auth", "true");
        props.put("mail.smtp.ssl.enable", "true");
        session = Session.getInstance(props, new Authenticator() {
            @Override
            protected PasswordAuthentication getPasswordAuthentication() {
                return new PasswordAuthentication(FROM, AUTI_CODE);
            }
        });
        //开启debug模式
        session.setDebug(true);
    }

    public static void sendEmail(String to, String text){
        try{
            Message message = new MimeMessage(session);
            message.setFrom(new InternetAddress(FROM));
            message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(to));
            message.setSubject("智能工厂系统验证码");
            message.setContent("<h2>您正在使用邮箱验证码重置密码</h2><p>验证码为: <strong>" + text + "</strong></p><p>验证码5分钟内有效，请尽快使用。</p>", "text/html;charset=utf-8");
            Transport.send(message);
        }catch(MessagingException e){
            e.printStackTrace();
        }
    }

    public static void main(String[] args) {
        sendEmail("2955097528@qq.com", "123456");
    }
    
}